from time import time
import sys
sys.path.insert(0,'../..')
from rmpoly import rgens
try:
  from gmpy import mpz
except:
  mpz = int

# faster with int
#mpz = int

try:
  n = int(sys.argv[1])
  kt = int(sys.argv[2])
  bits_exp = int(sys.argv[3])
except:
  print '''prog n kt bits_exp
  variant of Fateman benchmark with truncation of monomials
  with total degree >= kt
  p = (1 + x + y + z + t + O(_t^kt))**n
  bench p*(p+1)
  '''
  sys.exit()

assert 4*n < 2**bits_exp
# use grlex, with hidden variable _t carrying the total degree
rp,t,z,y,x = rgens('t,z,y,x', bits_exp, mpz,order='grlex')
p1 = 1+x+y+z+t
t1 = time()
p1 = p1.pow_trunc(n,'_t',kt)
p2 = p1 + 1

h = p1.mul_trunc(p2,'_t',kt)
t2 = time()
print '%s p1*p2 t=%.2f' %(sys.argv,t2 - t1)
#print h.coefficient(x**2*y*z)
